package com.mc.lp.database.service.handler.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.springframework.util.CollectionUtils;
/**
 * 
 * @author deepak
 * @param <O>
 * @param <I>
 * creates groups 
 */
public class Group{
	public static <I, O> HashMap<O, List<I>> unique(Collection<I> incomingCollection, GroupBy<I,O> groupBy ){
		if(CollectionUtils.isEmpty(incomingCollection) || groupBy == null){
			return null;
		}
		
		else {
			HashMap<O, List<I>> resultMap = new HashMap<O, List<I>>();
			for(I in: incomingCollection){
				O o= groupBy.groupBy(in);
				List<I> existing=resultMap.get(o);
				
				if(existing ==null){
					existing = new ArrayList<I>();
				}
				existing.add(in);
				resultMap.put(o, existing);
			}
			return resultMap;
		}
	}
}